/**
 * Created by x on 17-7-12.
 */
angular.module('tribalc').controller(
    'apartmentManageController',
    ['$rootScope', '$scope', '$state', '$stateParams', 'Enum','Api','$location','Alert','ApartmentInfoModel',
        function ($rootScope, $scope, $state, $stateParams, Enum,Api,$location,Alert,ApartmentInfoModel) {

            $scope.showcompanyDetails = showcompanyDetails;
            // $scope.prevPageClick = prevPageClick;
            // $scope.nextPageClick = nextPageClick;
            $scope.getReallyPicturePaths = getReallyPicturePaths;
            $scope.pageNumber = 1;
            $scope.getApartManageStatus = getApartManageStatus;

            getCompanyInfo();
            function getCompanyInfo() {
                Api.getCompanyDetail(null,function (result) {
                    $scope.communityId = result.data.communityId;
                    getApartmentList($scope.pageNumber);
                });
            }

            function getApartmentList(pageNum) {
                var size = 4;
                Api.getApartmentList($scope.communityId,pageNum,size,function (result) {
                    function getPageTotal(total,size){
                        return  Math.ceil(total/size)
                    }
                    var pagination = new Pagination($('#paginationContainer'),result.data.pageNum, getPageTotal(result.data.totalSize, size));
                    pagination.init();
                    pagination.action = function (pageNum) {
                        getApartmentList(pageNum)
                    }
                    
                    if (result.data.content.length > 0)
                    {
                        $scope.companyDetails = result.data.content;
                    }
                    // else if ($scope.pageNumber !== 1)
                    // {
                    //     $scope.pageNumber --;
                    //     Alert.alertBox('已经是最后一页了');
                    // }

                });
            }

            // function prevPageClick() {
            //     if ($scope.pageNumber > 1)
            //     {
            //         $scope.pageNumber --;
            //         getApartmentList($scope.pageNumber,1);
            //     }
            //     else
            //     {
            //         Alert.alertBox('已经是第一页了');
            //     }
            // }
            //
            // function nextPageClick() {
            //     $scope.pageNumber ++;
            //     getApartmentList($scope.pageNumber,1);
            // }


            function getReallyPicturePaths(pictures) {
                if (!pictures) return [];
                Array.from(pictures).forEach(function (path, index) {
                    pictures[index] = formatReallyPicturePath(path);
                });
                return pictures;
            }

            function formatReallyPicturePath(path) {
                var oss = 'oss://';
                if (path && path.indexOf(oss) !== -1) {
                    path = Api.ALIYUN_URL + path.split(oss)[1];
                }
                return path;
            }

            $scope.addApartmentInfoPage=function () {
                $state.go('company.companyaddApartmentInfo' );
            };

            function showcompanyDetails(conent) {

                $state.go('company.companyApartmentInfo',{apartmentId:conent.id});

            }


            function getApartManageStatus(status){
                return {'OFFLINE':'下线','ONLINE':'在线','CHECK':'已入住','FREE':'空闲'}[status]
            }

        }
    ]);